<?php 

class Inventory extends CI_Controller {

 function __construct() {
   parent::__construct();
   $this->load->model('inventory_model');
   $this->load->database();
   $this->load->helper('url');
 }

 //Loads the users view
 function index() {
   if($this->session->userdata('logged_in')) {
     $session_data = $this->session->userdata('logged_in');
     $data['ds_usuario'] = $session_data['ds_usuario'];
     $this->load->view('inventory_view', $data);
   } else {
     redirect('login', 'refresh');
   }
 }

function savearticleunity() {
  if($this->input->post("articlename")&&$this->input->post("articletype")
     &&$this->input->post("articledescription")){
    if(isset($_FILES['file'])){
      $file_element_name = "file";
      $articleid = $this->input->post("articleid");
      $articlename = $this->input->post("articlename");
      $articletype = $this->input->post("articletype");
      $articledescription = $this->input->post("articledescription");
      $articlefeatures = $this->input->post("articlefeatures");
      $articlecomments = $this->input->post("articlecomments");
      $config['upload_path'] = './assets/articles-images/';
      $config['allowed_types'] = 'gif|jpg|png|jpeg';
      $config['max_size'] = 1024 * 8;
      $config['encrypt_name'] = TRUE;

      $this->load->library('upload', $config);

      if (!$this->upload->do_upload($file_element_name)) {
          echo json_encode(array("response" => "failed"));
      } else {
          $data = $this->upload->data();
          //query the database
          $result = $this->articles_model->savearticle($articleid, $articlename, $articletype, $articledescription, $articlefeatures, $articlecomments, $data['file_name']);
          if($result) {
            echo json_encode(array("response" => "success"));
          } else {
            echo json_encode(array("response" => "failed"));
          }
      }
      @unlink($_FILES[$file_element_name]);
    } else {
      $articleid = $this->input->post("articleid");
      $articlename = $this->input->post("articlename");
      $articletype = $this->input->post("articletype");
      $articledescription = $this->input->post("articledescription");
      $articlefeatures = $this->input->post("articlefeatures");
      $articlecomments = $this->input->post("articlecomments");
      $fakeFile = "";
      $result = $this->articles_model->savearticle($articleid, $articlename, $articletype, $articledescription, $articlefeatures, $articlecomments, $fakeFile);
      if($result) {
        echo json_encode(array("response" => "success"));
      } else {
        echo json_encode(array("response" => "failed"));
      }
    }
  } else {
      echo json_encode(array("response" => "incomplete_form"));
  }
}

}
?>